/* lfsystest.c - lfsystest.c */

#include <xinu.h>
#include <stdio.h>
#include <string.h>

/*------------------------------------------------------------------------
 * lfsystest - test local file system
 *------------------------------------------------------------------------
 */

char input[100], output[100];
int i;

int32 test(did32 fd, struct lflcblk *lfptr, struct ldentry *dirptr) {
//int32 test(did32 fd) {
	for(i=0;i<100;i++) {
		input[i] = (char)(i%256);
		output[i] = ' ';
	}
	/* kprintf("\r\nIntially:\r\n");
	kprintf("\r\nfd = %d\r\n", fd);
	kprintf("lfdev = %d\r\n", lfptr->lfdev);
	kprintf("lfname = %s\r\n", lfptr->lfname);
	kprintf("ID of current index block = %d\r\n", lfptr->lfinum);
	kprintf("ID of current data block = %d\r\n", lfptr->lfdnum);
	kprintf("Byte position of next byte = %d\r\n", lfptr->lfpos);
	kprintf("dirptr->ld_size = %d\r\n", dirptr->ld_size);
	kprintf("dirptr->ld_ilist = %d\r\n", dirptr->ld_ilist);
	kprintf("dirptr->ld_name = %s\r\n", dirptr->ld_name);
	kprintf("dirptr->ld_type = %d\r\n", dirptr->ld_type); */
	write(fd, input, 100);
	/* kprintf("\r\nAfter write(fd, input, 100):\r\n");
	kprintf("ID of current index block = %d\r\n", lfptr->lfinum);
	kprintf("ID of current data block = %d\r\n", lfptr->lfdnum);
	kprintf("Byte position of next byte = %d\r\n", lfptr->lfpos);
	kprintf("dirptr->ld_size = %d\r\n", dirptr->ld_size);
	kprintf("dirptr->ld_ilist = %d\r\n", dirptr->ld_ilist);
	kprintf("dirptr->ld_name = %s\r\n", dirptr->ld_name);
	kprintf("dirptr->ld_type = %d\r\n", dirptr->ld_type); */
	seek(fd, 0);
	/* kprintf("\r\nAfter seek(fd, 0):\r\n");
	kprintf("ID of current index block = %d\r\n", lfptr->lfinum);
	kprintf("ID of current data block = %d\r\n", lfptr->lfdnum);
	kprintf("Byte position of next byte = %d\r\n", lfptr->lfpos);
	kprintf("dirptr->ld_size = %d\r\n", dirptr->ld_size);
	kprintf("dirptr->ld_ilist = %d\r\n", dirptr->ld_ilist);
	kprintf("dirptr->ld_name = %s\r\n", dirptr->ld_name);
	kprintf("dirptr->ld_type = %d\r\n", dirptr->ld_type); */
	read(fd, output, 100);
	/* kprintf("\r\nAfter read(fd, output, 100):\r\n");
	kprintf("ID of current index block = %d\r\n", lfptr->lfinum);
	kprintf("ID of current data block = %d\r\n", lfptr->lfdnum);
	kprintf("Byte position of next byte = %d\r\n", lfptr->lfpos);
	kprintf("dirptr->ld_size = %d\r\n", dirptr->ld_size);
	kprintf("dirptr->ld_ilist = %d\r\n", dirptr->ld_ilist);
	kprintf("dirptr->ld_name = %s\r\n", dirptr->ld_name);
	kprintf("dirptr->ld_type = %d\r\n\r\n", dirptr->ld_type); */
	for(i=0;i<100;i++) {
		if (input[i] != output[i]) {
			printf("Wrong");
			return SYSERR;
		}
	}
	return OK;
}

syscall lfsystest(void)
{
	did32 fd1, fd2, fd3, fd4;
	//did32 fd;
	struct lflcblk *lfptr1, *lfptr2, *lfptr3, *lfptr4;
	struct	ldentry	*dirptr1, *dirptr2, *dirptr3, *dirptr4;
	char *fn1, *fn2, *fn3, *fn4;
	//char fn[81];
	//int32 i,j,k;
	int32 retval;
	fn1 = "/abc";
	fn2 = "/abc/def";
	fn3 = "/abc/def/jkl";
	fn4 = "/abc/def/ghi";

	open(LF_DISK_DEV, "1000", NULL);
	control(LFILESYS, LFS_CTL_FORMAT, 100, 512000);
	lfsckfmt(LF_DISK_DEV);
		
	fd1 = control(LFILESYS, LFS_CTL_MKDIR, (int)fn1, 0);
	//fd1 = open(LFILESYS, fn1, "rw");
	sleepms(10);
	if(fd1 == SYSERR) {
		kprintf("fd1 open fails!\r\n");
		return SYSERR;
	}
	lfptr1 = &lfltab[devtab[fd1].dvminor];
	dirptr1 = lfptr1->lfdirptr;
	sleepms(10);
	close(fd1);
	kprintf("Open fd1 done\r\n\r\n");
	/* retval = test(fd1, lfptr1, dirptr1);
	if (retval == SYSERR) {
		printf("fd1 test fails!\r\n");
		return SYSERR;
	}
	else {
		printf("fd1 test OK!\r\n\r\n");
	} */
	
	fd2 = control(LFILESYS, LFS_CTL_MKDIR, (int)fn2, 0);
	//fd2 = open(LFILESYS, fn2, "rw");
	sleepms(10);
	if(fd2 == SYSERR) {
		kprintf("fd2 open fails!\r\n\r\n");
		return SYSERR;
	}
	lfptr2 = &lfltab[devtab[fd2].dvminor];
	dirptr2 = lfptr2->lfdirptr;
	close(fd2);
	kprintf("Open fd2 done\r\n\r\n");
	/* retval = test(fd2, lfptr2, dirptr2);
	if (retval == SYSERR) {
		printf("fd2 test fails!\r\n");
		return SYSERR;
	}
	else {
		printf("fd2 test OK!\r\n\r\n");
	}
	sleepms(10); */
	
	//fd3 = control(LFILESYS, LFS_CTL_MKDIR, (int)fn3, 0);
	fd3 = open(LFILESYS, fn3, "rw");
	sleepms(10);
	if(fd3 == SYSERR) {
		kprintf("fd3 open fails!\r\n\r\n");
		return SYSERR;
	}
	lfptr3 = &lfltab[devtab[fd3].dvminor];
	dirptr3 = lfptr3->lfdirptr;
	close(fd3);
	kprintf("Open fd3 done\r\n\r\n");
	/* retval = test(fd3, lfptr3, dirptr3);
	if (retval == SYSERR) {
		printf("fd3 test fails!\r\n");
		return SYSERR;
	}
	else {
		printf("fd3 test OK!\r\n\r\n");
	}
	sleepms(10); */
	
	
	//fd4 = control(LFILESYS, LFS_CTL_MKDIR, (int)fn4, 0);
	fd4 = open(LFILESYS, fn4, "rw");
	sleepms(10);
	if(fd4 == SYSERR) {
		kprintf("fd4 open fails!\r\n\r\n");
		return SYSERR;
	}
	lfptr4 = &lfltab[devtab[fd4].dvminor];
	dirptr4 = lfptr4->lfdirptr;
	kprintf("Open fd4 done\r\n\r\n");
	retval = test(fd4, lfptr4, dirptr4);
	if (retval == SYSERR) {
		printf("fd4 test fails!\r\n");
		return SYSERR;
	}
	else {
		printf("fd4 test OK!\r\n\r\n");
	}
	sleepms(10);
	close(fd4);
	
	retval = control(LFILESYS, LFS_CTL_RMDIR, (int)fn2, 0);
	if(retval == OK) {
		kprintf("rmdir %s done\r\n\r\n", fn2);
	}
	else {
		kprintf("rmdir %s fails\r\n\r\n", fn2);
	}
	
	retval = test(fd4, lfptr4, dirptr4);
	if (retval == SYSERR) {
		printf("fd4 test fails!\r\n\r\n");
		return SYSERR;
	}
	else {
		printf("fd4 test OK!\r\n\r\n");
	}
	/* for (i=0; i<26; i++) {
		for (j=0; j<40; j++) {
			fn[2*j] = '/';
			fn[2*j+1] = (char)(int)('a'+i);
			fn[2*j+2] = NULLCH;
			k = 0;
			while(fn[k]!= NULLCH) {
				kprintf("%c", fn[k++]);
			}
			kprintf("\r\n");
			fd = control(LFILESYS, LFS_CTL_MKDIR, (int)fn, 0);
			kprintf("open file %d done \r\n\r\n", i*40+j);
			close(fd);
		}
	} */
	
	kprintf("All test done!\r\n");
	return OK;
}
